<template>
  <div class="container">
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-form ref="form" :model="page" label-width="80px">
            <el-form-item label="关键字">
              <el-input
                placeholder="根据编号搜索"
                size="small"
                v-model="page.keyword"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="18">
          <div style="text-align: right">
            <el-button size="small" @click="page.keyword=''">清除</el-button>
            <el-button size="small" type="primary" @click="hGetData">搜索</el-button>
          </div>
        </el-col>
      </el-row>
      <TotalityAdd :consts="counts" />
      <el-row>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号" width="220">
          </el-table-column>
          <el-table-column label="题型" width="80">
            <template #default="{ row }">
              {{ stems(row.questionType) }}
            </template>
          </el-table-column>
          <el-table-column label="题目编号" width="220">
            <template #default="{ row }">
              <div v-for="(itm, i) in row.questionIDs" :key="i">
                <a @click="DiongShow(itm.id)" style="color: rgb(0, 153, 255)" href="#">{{ itm.number }}</a>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间" width="180">
          </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)">
          </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="{ row }">
              <el-button
                @click="deletes(row.id)"
                type="danger"
                icon="el-icon-delete"
                plain
                circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          background
          :page-sizes="[20,30, 50,100]"
          :page-size="20"
          :total="counts"
          layout="prev, pager, next,sizes, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
    <SpringDieng :dialog-visible="dialogVisible" @handle-close="handleClose" :dialog-data="dialogData"/>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import SpringDieng from './QuestionsList/SpringDieng'
export default {
  components: { SpringDieng },
  data () {
    return {
      tableData: [],
      page: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      counts: null,
      currentPage4: 1,
      dialogVisible: false,
      dialogData: {}
    }
  },
  mounted () {
    this.hGetData()
  },
  methods: {
    async DiongShow (id) {
      const res = await detail({ id })
      this.dialogData = res.data
      this.dialogVisible = true
    },
    handleClose () {
      this.dialogVisible = false
    },
    // 控制条数
    handleSizeChange (val) {
      this.page.pagesize = val
      this.hGetData()
    },
    // 控制页数
    handleCurrentChange (val) {
      this.page.page = val
      this.hGetData()
    },
    async hGetData () {
      const newObj = {}
      for (const key in this.page) {
        if (this.page[key]) {
          newObj[key] = this.page[key]
        }
      }
      const res = await randoms(newObj)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    deletes (id) {
      this.$confirm('确定删除题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeRandoms({ id })
          this.counts--
          this.tableData = this.tableData.filter((itm) => itm.id !== id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 15px;
  .block {
    margin-top: 20px;
    text-align: right;
    ::v-deep .el-pagination__jump {
      margin: 0;
    }
  }
}
</style>
